package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@RestController
//@Scope("prototype")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping("/depts")
    public Result findAll () {

        List<Dept> lists = deptService.list();
        return Result.success(lists);

    }

    @DeleteMapping("/depts")

    public Result deleteById ( @RequestParam/*接收Query 格式*/(value = "id", defaultValue = "2") Integer deptId ) {
        deptService.deleteById(deptId);
        return Result.success();

    }

    @PostMapping("/depts")

    public Result add ( @RequestBody/*接受json格式*/ Dept dept ) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptService.save(dept);

        return Result.success();
    }

    @GetMapping("/depts/{id}")
    public Result getById ( @PathVariable/*获取路径参数*/("id") Integer deptId ) {
        Dept dept = deptService.getById(deptId);
        return Result.success(dept);

    }

    @PutMapping("/depts")
    public Result update ( @RequestBody Dept dept ) {
        dept.setUpdateTime(LocalDateTime.now());
        deptService.updateById(dept);
        return Result.success();
    }
}